# Copyright (c) 2007, Enrico Franchi
# All rights reserved.
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#     * Neither the name of the University of California, Berkeley nor the
#       names of its contributors may be used to endorse or promote products
#       derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

from PyObjCTools import NibClassBuilder
from Foundation import NSMakeRange


import models
import configuration

class PortInfoController(NibClassBuilder.AutoBaseClass):
    def awakeFromNib(self):
        self.buildsTableView.setTarget_(self)
        self.librariesTableView.setTarget_(self)
        self.runtimesTableView.setTarget_(self)
        self.buildsTableView.setDoubleAction_("doubleClickBuilds:")
        self.librariesTableView.setDoubleAction_("doubleClickLibraries:")
        self.runtimesTableView.setDoubleAction_("doubleClickRuntimes:")
        
    def selectPort(self, name):
        self.model = models.PortModelFactory.createPortModel(configuration.port_path(), name)
        self.refreshView()
        
    def refreshView(self):
        self.nameField.setStringValue_(self.model.name)
        self.versionField.setStringValue_(self.model.version)
        self.revisionField.setStringValue_(self.model.revision)
        self.setHomepage()
        self.setDescription()
        self.platformsField.setStringValue_(' '.join(self.model.platforms))
        self.maintainersField.setStringValue_(' '.join(self.model.maintainers))
        self.variantsTableView.reloadData()
        self.buildsTableView.reloadData()
        self.librariesTableView.reloadData()
        self.runtimesTableView.reloadData()
        
    def setHomepage(self):
        homepage = self.model.homepage
        if len(homepage) > 30:
            homepage = "%s..." % homepage[:27]
        self.homepageField.setStringValue_(homepage)
        
    def setDescription(self):
        text = self.model.description
        ts = self.descriptionTextView.textStorage()
        ts.replaceCharactersInRange_withString_(
            NSMakeRange(0, len(ts)),
            text
        )
        
    def tableView_shouldEditTableColumn_row_(self, aTableView, aTableColumn, rowIndex):
        return False
    
    def doubleClickBuilds_(self, sender):
        self.commonTableViewDoubleClick('build_dependencies', sender)
    def doubleClickLibraries_(self, sender):
        self.commonTableViewDoubleClick('library_dependencies', sender)
    def doubleClickRuntimes_(self, sender):
        self.commonTableViewDoubleClick('runtime_dependencies', sender)
    def commonTableViewDoubleClick(self, kind, sender):
        index = sender.selectedRow()
        name = getattr(self.model, kind)[index]
        self.appDelegate.selectRowOfVisibleTableByName(name)    
        
    def numberOfRowsInTableView_(self, table):
        if not hasattr(self, 'model'): return 0
        if   table == self.variantsTableView:
            return len(self.model.variants)
        elif table == self.buildsTableView:
            return len(self.model.build_dependencies)
        elif table == self.librariesTableView:
            return len(self.model.library_dependencies)
        elif table == self.runtimesTableView:
            return len(self.model.runtime_dependencies)
            
    def tableView_objectValueForTableColumn_row_(self, table, tableColumn, row):
        if   table == self.variantsTableView:
            return self.model.variants[row]
        elif table == self.buildsTableView:
            return self.model.build_dependencies[row]
        elif table == self.librariesTableView:
            return self.model.library_dependencies[row]
        elif table == self.runtimesTableView:
            return self.model.runtime_dependencies[row]
            
    def selectedVariants(self):
        variants = self.variantsTableView.selectedRowEnumerator()
        variant_names = [self.model.variants[number] for number in variants]
        return variant_names